﻿using System;
using System.Collections.Generic;
using Jayden.Dll.Web.Enums;
using Jayden.Dll.Web.HtmlControls.Descriptions;
using Jayden.Dll.Web.HtmlControls.Generics;

namespace Jayden.Dll.Web.HtmlControls
{
	public class Tr : HtmlElement
	{
		public Tr() : base(TrDescription.Instance) { }

		public Td AddTd(string text = null, bool htmlEncode = true) { return AddControl(new Td(text, htmlEncode)) as Td; }
		public Th AddTh(string text = null, bool htmlEncode = true) { return AddControl(new Th(text, htmlEncode)) as Th; }

		public Td[] AddTd(params string[] cells)
		{
			List<Td> tds = new List<Td>();
			for (int c = 0; c < cells.Length; c++)
			{
				Td td = new Td();
				td.AddText(cells[c]);
				AddControl(td);
				tds.Add(td);
			}
			return tds.ToArray();
		}
		public Td[] AddTd(params HtmlControl[] controls)
		{
			List<Td> tds = new List<Td>();
			for (int c = 0; c < controls.Length; c++)
			{
				Td td = new Td();
				td.AddControl(controls[c]);
				AddControl(td);
				tds.Add(td);
			}
			return tds.ToArray();
		}

		public Th[] AddTh(params string[] cells)
		{
			List<Th> tds = new List<Th>();
			for (int c = 0; c < cells.Length; c++)
			{
				Th td = new Th();
				td.AddText(cells[c]);
				AddControl(td);
				tds.Add(td);
			}
			return tds.ToArray();
		}
		public Th[] AddTh(params HtmlControl[] controls)
		{
			List<Th> tds = new List<Th>();
			for (int c = 0; c < controls.Length; c++)
			{
				Th td = new Th();
				td.AddControl(controls[c]);
				AddControl(td);
				tds.Add(td);
			}
			return tds.ToArray();
		}
	}
	public class TrDescription : HtmlElementDescription
	{
		public static readonly TrDescription Instance = new TrDescription();
		private TrDescription() : base("tr", HtmlVersion.Html4, true, true) { }
		public override Type ControlType { get { return typeof(Tr); } }
		public override string Description { get { return "row of cells in a table."; } }
		public override bool AllowInnerText { get { return false; } }
		public override IEnumerable<HtmlElementDescription> PermittedParents
		{
			get
			{
				yield return TableDescription.Instance;
				yield return THeadDescription.Instance;
				yield return TBodyDescription.Instance;
				yield return TFootDescription.Instance;
			}
		}
		public override IEnumerable<HtmlElementDescription> PermittedElements
		{
			get
			{
				yield return TdDescription.Instance;
				yield return ThDescription.Instance;
			}
		}
	}
}
